package com.itheima.service;


import com.itheima.common.JsonResult;
import com.itheima.mapper.admin.OrderMapper;
import com.itheima.pojo.admin.Order;
import com.itheima.pojo.admin.Pagination;
import com.itheima.pojo.admin.orderdto.OrderPaginationDto;
import com.itheima.pojo.admin.orderdto.OrderStatusUpdateByIdDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ChenJunJie
 * @date 2022/6/14 14:30
 * 订单的service层
 */
@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    // 订单分页查询
    public JsonResult<Pagination<Order>> orderPagination (OrderPaginationDto dto){

        Pagination<Order> eps = new Pagination<Order>();

        //获取当前页数
        eps.setCurrent(dto.getPage());


        //查询订单分页
        Integer page = dto.getPage();
        Integer index = (page-1) * dto.getPageSize();
        dto.setPage(index);
        List<Order> orders = orderMapper.orderPagination(dto);

        //查询订单数据
        List<Order> ordersInfo = orderMapper.orderSelectByTimeAndNumber(dto);

        //获取总条数
        eps.setRecords(orders);
        //获取需要展示的总个数
        eps.setTotal(ordersInfo.size());
        //获取每条展示几页
        eps.setSize(dto.getPageSize());

        if (orders.size()<=0||ordersInfo.size()<=0){
            return JsonResult.employeePaginationSuccess(eps,"暂无数据");
        }

        return JsonResult.employeePaginationSuccess(eps, "查询成功");
    }

    //修改订单状态
    public boolean orderStatusUpdateById(OrderStatusUpdateByIdDto orderStatus){
        int i = orderMapper.orderStatusUpdateById(orderStatus);

        return i>0;
    }
}
